Computer programs are generally written in a high-level programming language (e.g., Pascal and C). Compilers are then used to translate the instructions of the high-level programing language into machine instructions, which can be executed by a computer. The compilation process is generally divided into 6 phases:
1. Lexical analysis PA1 2. Syntactic analysis PA1 3. Semantic analysis PA1 4. Intermediate code generation PA1 5. Code optimization PA1 6. Final code generation
Lexical analysis involves scanning the computer program and recognizing components or tokens of the high-level language. During this phase, the compiler converts the computer program into a series of tokens that are processed during syntactic analysis. For example, during lexical analysis, the compiler would recognize the statement EQU cTable=1.0;
as the variable(cTable), the operator(=), the constant(1.0), and a semicolon. A variable, operator, constant, and semicolon are tokens of the high-level language. During syntactic analysis, the compiler processes the tokens and generates a syntax tree to represent the program. A syntax tree is a tree structure in which operators and operands are stored in nodes. In the above example, the operator (=) has two operands: the variable (cTable) and the constant (1.0). During semantic analysis, the compiler modifies the syntax tree to ensure semantic correctness. For example, if the variable (cTable) is an integer and the constant (1.0) is real, then during semantic analysis an operator for real to integer conversion would be added to the syntax tree. During intermediate code generation, code optimization, and final code generation, the compiler generates machine instructions to implement the program represented by the syntax tree. The machine instructions can then be executed by the computer.